package ca.kooki.codesnippet.views;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;

import ca.kooki.codesnippet.Activator;
import ca.kooki.codesnippet.Tag;

public class TagEntryView extends ViewPart {
  
  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````

  private Action aboutAction;
  
  public Label labelTitle;
  public Label labelKeywords;
  public Label labelCode;
  public Label labelMessage;
  
  public Text inputTitle;
  public Text inputKeywords;
  public Text inputCode;

  public Button buttonAdd;
  
  public Composite parent;
  
  
  private TagEntryViewHandler _handler;
  
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````
  
  public TagEntryView()
  {}
  
  //```````````````````````
  // INTERFACE
  //```````````````````````
  
  public void createPartControl(Composite parent) 
  {
    initLayout(parent);
    addAboutUs();
    hookContextMenu();
    contributeToActionBars();
  }

  public void initLayout(Composite parent)
  {
    labelTitle = createLabel(parent,"Title");
    inputTitle = new Text(parent,SWT.LEFT | SWT.SINGLE | SWT.BORDER);

    labelKeywords = createLabel(parent,"Keywords");
    inputKeywords = new Text(parent,SWT.LEFT | SWT.SINGLE | SWT.BORDER);

    labelCode = createLabel(parent,"Code");
    inputCode = new Text(parent,SWT.LEFT | SWT.MULTI | SWT.BORDER);
    
    skipEntry(parent);
    buttonAdd = new Button(parent,SWT.PUSH);
    buttonAdd.setText("Add");
    
    labelMessage = new Label(parent,SWT.LEFT);
    
    GridLayout viewLayout = new GridLayout();
    viewLayout.numColumns = 3;

    GridData layoutData = null;

    layoutData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END);
    labelTitle.setLayoutData(layoutData);

    layoutData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END);
    labelKeywords.setLayoutData(layoutData);

    layoutData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END);
    labelCode.setLayoutData(layoutData);
    
    layoutData = new GridData(GridData.FILL_HORIZONTAL);
    layoutData.horizontalSpan = 2;
    inputTitle.setLayoutData(layoutData);

    layoutData = new GridData(GridData.FILL_HORIZONTAL);
    layoutData.horizontalSpan = 2;
    inputKeywords.setLayoutData(layoutData);

    layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.horizontalSpan = 2;
    inputCode.setLayoutData(layoutData);

    layoutData = new GridData(GridData.FILL_HORIZONTAL);
    labelMessage.setLayoutData(layoutData);

    _handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    inputTitle.addKeyListener(_handler);
    inputKeywords.addKeyListener(_handler);
    inputCode.addKeyListener(_handler);
    addNewSnippetButtonListener(_handler);
    
    parent.setLayout (viewLayout);
    this.parent = parent;
    
  }

  public void setFocus() {
    inputTitle.setFocus();
  }

  public void tagExistingCodeSnippet(Tag tag)
  {
    _handler.tagExistingCodeSnippet(tag);
  }
  
  public void updateTag(Tag tag)
  {
    _handler.updateTag(tag);
  }

  
  //```````````````````````
  // PRIVATE METHODS
  //```````````````````````

  private Label createLabel(Composite parent, String text)
  {
    Label myLabel = new Label(parent, SWT.RIGHT);
    myLabel.setText(text);
    return myLabel;
  }

  private void hookContextMenu() {
    MenuManager menuManager = new MenuManager("#PopupMenu");
    menuManager.setRemoveAllWhenShown(true);
    menuManager.addMenuListener(new IMenuListener() {
      public void menuAboutToShow(IMenuManager manager) {
        TagEntryView.this.fillContextMenu(manager);
      }
    });
    
    Menu menu = menuManager.createContextMenu(parent);
    parent.setMenu(menu);
  }

  private void contributeToActionBars() {
    IActionBars bars = getViewSite().getActionBars();
    fillLocalToolBar(bars.getToolBarManager());
  }

  private void fillContextMenu(IMenuManager manager) {
    manager.add(aboutAction);
    manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
  }
  
  private void fillLocalToolBar(IToolBarManager manager) {
    manager.add(aboutAction);
  }

  private void addAboutUs() {
    aboutAction = new Action() {
      public void run() {
        String message = "Andrew Forward, 1484511\nTerm Project, CodeSnippets\nDue: Dec 14, 2006\nCOMP 5900 / CSI 5140\nDwight Deugo and Sonia Chiasson\n";
        showMessage(message);
      }
    };
    aboutAction.setText("About the Author");
    aboutAction.setToolTipText("Andrew Forward, 1484511");
    aboutAction.setImageDescriptor(Activator.getImageDescriptor("icons/aforward.gif"));
    
  }

  private void showMessage(String message) {
    MessageDialog.openInformation(shell(),"CodeSnippetEntryView List",message);
  }
    
  private Shell shell()
  {
    return inputTitle.getShell();
  }
  
  private void skipEntry(Composite parent)
  {
    Label empty = createLabel(parent,"");
    empty.setVisible(false);
  }
  
  private void addNewSnippetButtonListener(final TagEntryViewHandler hanlder)
  {
    buttonAdd.addSelectionListener(new SelectionAdapter()
    {
      public void widgetSelected(SelectionEvent event)
      {
        hanlder.addNewTag();
      }
    }
    );
  }


}